﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Messaging;
using System.Text;
using NsbMonitorClientService.QueueManager;
using NUnit.Framework;

namespace NsbMonitorClientService.Tests.QueueReader
{
    [TestFixture]
    class can_read_from_queue_test  : with_local_test_queue
    {

        private const string Label1 = "Test Message Label 1";
        private const string Label2 = "Test Message Label 2";
        private const string Body1 = " a b c d e f g h /";
        private const string Body2 = " a b c d e f g h i /";

        private BasicQueueReader _sut;

        [SetUp]
        public void Before()
        {

            LocalTestQueue1.Send(Body1, Label1);
            LocalTestQueue1.Send(Body2, Label2);

            _sut = new BasicQueueReader();

        }

        [Test]
        public void can_read_messages()
        {

            IEnumerable<Message> messages = _sut.ReadMessagesFromQueue(LocalTestQueue1Name);
            
            Assert.NotNull(messages, "messages are null");
            Assert.AreEqual(2, messages.Count(), "messages size is different than expected");

            var first = messages.First();

            Console.WriteLine(first.BodyStream.Length);
            Console.WriteLine(first.Label);
            Console.WriteLine(first.Id);
            Console.WriteLine(first.SentTime);

            using (StreamReader reader = new StreamReader(first.BodyStream))
            {
                string observedBody = reader.ReadToEnd();
                Console.WriteLine("observedBody: " + observedBody);
            }

            Assert.AreEqual(Label1, first.Label);
            Assert.NotNull(first.Id);
            Assert.NotNull(first.SentTime);
        }


    }
}
